const express= require("express")

const cors = require('cors')
let app = express("app")


app.use(cors())

//导入路由

// 用户个人信息的路由
const my = require("./router/my")

const homepage = require("./router/homePage")
const upload = require("./router/upload")
const alongUpload = require("./router/alongUpload")
const shop = require("./router/shop")
app.use("/image",express.static("static/image"))
app.use("/homePage",homepage)
app.use("/api",upload)
app.use("/alongImage",alongUpload)
app.use("/shop",shop)
app.use("/my",my)


// app.all('*', function (req, res, next) {
//     res.header('Access-Control-Allow-Origin', '*')
//     res.header('Access-Control-Allow-Headers', 'Origin,X-Requested-With,Accept,Content-type')
//     res.header('Access-Control-Allow-Credentials', true)
//     res.header('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS')
//     res.header('Content-Type', 'application/json;charset=utf-8')
//     if (req.method.toLowerCase() == 'options') res.sendStatus(200)
//     else next()
//   })


var http = require('http').Server(app);

var io = require('socket.io')(http,{cors:true});

io.on('connect', (socket) => {
    console.log('有人连接成功了')
    // 后台发起的接收订单
    socket.on('my other event', function (data) {
      // console.log(data)
      // console.log("已回复")
      socket.broadcast.emit(data.openId,data)
    })
    // 用户连接
    socket.emit('open', {data:'恭喜你收到了信息'})
  

    // 用户发起订单
    socket.on("pay",()=>{
      console.log("泥马死了")
      socket.broadcast.emit('send1','奶奶的')
    })

     // 监听客户端断开
    socket.on('disconnect', () => {
      console.log('客户端断开')
    })
  })
 



http.listen(1730,()=>{
    console.log("server running at http:127.0.0.1:1730")
})
// let port = 3000

// const server = require('http').createServer();

// const io = require('socket.io')(server, {
//     path: '/test',
//     serveClient: false,
//     // below are engine.IO options
//     pingInterval: 10000,
//     pingTimeout: 5000,
//     cookie: false
//   });

// io.on('connection',(socket)=>{
//     console.log("连接成功")
// })